/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doan2database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author DongVan
 */
public class NguoiDung {
    private int m_IDNguoiDung;
	private String m_Password;
	private String m_TenND;
	private String m_CMND;
	private String m_SoDT;
	private String m_PublicKey;
	private int m_LoaiND;
        private String m_UserName;

    public String getM_UserName() {
        return m_UserName;
    }

    public void setM_UserName(String m_UserName) {
        this.m_UserName = m_UserName;
    }
	public int getM_LoaiND() {
		return m_LoaiND;
	}
	public void setM_LoaiND(int m_LoaiND) {
		this.m_LoaiND = m_LoaiND;
	}
	public int getM_IDNguoiDung() {
		return m_IDNguoiDung;
	}
	public void setM_IDNguoiDung(int m_IDNguoiDung) {
		this.m_IDNguoiDung = m_IDNguoiDung;
	}
	public String getM_Password() {
		return m_Password;
	}
	public void setM_Password(String m_Password) {
		this.m_Password = m_Password;
	}
	public String getM_TenND() {
		return m_TenND;
	}
	public void setM_TenND(String m_TenND) {
		this.m_TenND = m_TenND;
	}
	public String getM_CMND() {
		return m_CMND;
	}
	public void setM_CMND(String m_CMND) {
		this.m_CMND = m_CMND;
	}
	public String getM_SoDT() {
		return m_SoDT;
	}
	public void setM_SoDT(String m_SoDT) {
		this.m_SoDT = m_SoDT;
	}
	public String getM_PublicKey() {
		return m_PublicKey;
	}
	public void setM_PublicKey(String m_PublicKey) {
		this.m_PublicKey = m_PublicKey;
	}

	public NguoiDung(int m_IDNguoiDung, String m_Password, String m_TenND,
			String m_CMND, String m_SoDT, String m_PublicKey, int loaiND,String m_UserName) {
		this.m_IDNguoiDung = m_IDNguoiDung;
		this.m_Password = m_Password;
		this.m_TenND = m_TenND;
		this.m_CMND = m_CMND;
		this.m_SoDT = m_SoDT;
		this.m_PublicKey = m_PublicKey;
		this.m_LoaiND = loaiND;
                this.m_UserName = m_UserName;
	}
	public NguoiDung(){
		
	}
	
	AESEncrypt aes = new AESEncrypt();
	Datapro dataprovider = new Datapro();

	
	public ArrayList<NguoiDung> selected() {
		String sqlQuery = "select * from NGUOI_DUNG";
		ArrayList<NguoiDung> listND = new ArrayList<NguoiDung>();
		ResultSet rs = dataprovider.Selected(sqlQuery);
		try {
			while (rs.next()) {
				NguoiDung nguoiDung = new NguoiDung();
				nguoiDung.m_IDNguoiDung = rs.getInt(1);
				nguoiDung.m_Password = aes.decryptString(rs.getString(2));
				nguoiDung.m_TenND = aes.decryptString(rs.getString(3));
				nguoiDung.m_CMND = aes.decryptString(rs.getString(4));
				nguoiDung.m_SoDT = aes.decryptString(rs.getString(5));
				nguoiDung.m_PublicKey = aes.decryptString(rs.getString(6));
				nguoiDung.m_LoaiND = rs.getInt(7);
                                nguoiDung.m_UserName = aes.decryptString(rs.getString(8));
				listND.add(nguoiDung);
			}
			dataprovider.disconnect();
	        rs.close();
	        return listND;
	        
	    } catch ( SQLException ex) {
	        Logger.getLogger(Datapro.class.getName()).log(Level.SEVERE, null, ex);
	    }
		return null;
	}
	

	public boolean insert() {
		String password = aes.encryptString(this.m_Password);
		String tenND = aes.encryptString(this.m_TenND);
		String cmnd = aes.encryptString(this.m_CMND);
		String soDT = aes.encryptString(this.m_SoDT);
		String publicKey = aes.encryptString(this.m_PublicKey);
		int loaiND = this.m_LoaiND;
                String userName = aes.encryptString(this.m_UserName);
		String query =  "insert into NGUOI_DUNG values('"+ password + "', '"+ tenND +"', '"+ cmnd +"', '"+ soDT +"', '"+ publicKey +"'," +loaiND+ ", '"+userName+"')";
		boolean result =  dataprovider.Insert(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean update(){
		int idND = this.m_IDNguoiDung;
		String password = aes.encryptString(this.m_Password);
		String tenND = aes.encryptString(this.m_TenND);
		String cmnd = aes.encryptString(this.m_CMND);
		String soDT = aes.encryptString(this.m_SoDT);
		String publicKey = aes.encryptString(this.m_PublicKey);
		int loaiND = this.m_LoaiND;
                String userName = aes.encryptString(this.m_UserName);
		String query = "update NGUOI_DUNG set Password = '"+ password +"', TenND = '"+tenND+"', CMND = '"+cmnd+"', SoDT = '"+soDT+"', PublicKey = '"+publicKey+"',LoaiND = "+loaiND+",UserName = '"+userName+"' where IDNguoiDung = " + idND + "\"";
		boolean result = dataprovider.Update(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean delete(){
		String query = "delete from NGUOI_DUNG where = " + this.m_IDNguoiDung + "\"";
		boolean result = dataprovider.Delete(query);
		dataprovider.disconnect();
		return result;
	}
}
